package com.open.capacity.nk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.open.capacity.commons.CodeEnum;
import com.open.capacity.commons.PageResult;
import com.open.capacity.finance.dto.audit_model_35.AuditModelStep1;
import com.open.capacity.finance.service.impl.AuditModelActuator35ServiceImpl;
import com.open.capacity.nk.dao.*;
import com.open.capacity.nk.dto.NKReceiptDetailDto;
import com.open.capacity.nk.dto.NKReceiptDto;
import com.open.capacity.nk.dto.NKReceiptTravelDto;
import com.open.capacity.nk.entity.NkApprovalpaymentreceipt;
import com.open.capacity.nk.entity.NkContractpaymentreceipt;
import com.open.capacity.nk.entity.NkDirectpaymentreceipt;
import com.open.capacity.nk.entity.NkPrepaidpaymentreceipt;
import com.open.capacity.nk.service.NkReimburseReceiptService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class NkReimburseReceiptServiceImpl implements NkReimburseReceiptService {

    @Autowired
    private NkReimburseReceiptDao receiptDao;



    /**
     * 分页
     *
     * @param params
     * @return
     */
    public PageResult<NKReceiptDetailDto> pageAll(Map<String, Object> params) {
        //设置分页信息，分别是当前页数和每页显示的总记录数【记住：必须在mapper接口中的方法执行之前设置该分页信息】
        if (MapUtils.getInteger(params, "page") != null && MapUtils.getInteger(params, "limit") != null)
            PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);

        params.put("quotaNames", AuditModelActuator35ServiceImpl.QUOTANAMES);
        List<NKReceiptDetailDto> list = receiptDao.findAll(params);
        PageInfo<NKReceiptDetailDto> pageInfo = new PageInfo(list);

        return PageResult.<NKReceiptDetailDto>builder().data(pageInfo.getList()).resp_code(CodeEnum.SUCCESS.getCode()).count(pageInfo.getTotal()).build();
    }

    /**
     * 列表
     *
     * @param params
     * @return
     */
    public List<NKReceiptDetailDto> findAll(Map<String, Object> params) {
        List<NKReceiptDetailDto> list = receiptDao.findAll(params);
        return list;
    }

    @Override
    public List<NKReceiptTravelDto> findTravel(Map<String, Object> params) {
        List<NKReceiptTravelDto> list = receiptDao.findTravel(params);
        return list;
    }

    @Override
    public List<NKReceiptDto> findQuota(Map<String, Object> params) {
        params.put("quotaNames", AuditModelActuator35ServiceImpl.QUOTANAMES);
        params.put("flowstate", AuditModelActuator35ServiceImpl.FLOWSTATE);
        List<NKReceiptDto> result = new ArrayList<>();
        List<NKReceiptDto> receiptDtos = new ArrayList<>();
        List<NKReceiptDetailDto> list = receiptDao.findAll(params);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(t -> {
                NKReceiptDto temp = new NKReceiptDto();
                temp.setIdorganization(t.getIdorganization());
                temp.setNameorganization(t.getNameorganization());
                temp.setIddepartment(t.getIddepartment());
                temp.setNamedepartment(t.getNamedepartment());
                temp.setQuotaname(t.getQuotaname());
                temp.setYearly(t.getYearly());
                temp.setPaymentperson(t.getPaymentperson());
                temp.setAmount(t.getAmount());
                receiptDtos.add(temp);
            });
        }

        //报销类型统计
        HashMap<String, List<NKReceiptDto>> collect = receiptDtos.stream().collect(Collectors.groupingBy(t -> t.getNameorganization() + "_" + t.getYearly() + "_" + t.getQuotaname(), HashMap::new, Collectors.toList()));

        for (Map.Entry<String, List<NKReceiptDto>> entry : collect.entrySet()) {
            String key = entry.getKey();
            if (key.split("_").length < 3) {
                continue;
            }

            NKReceiptDto step1 = new NKReceiptDto();
            step1.setQuotaname(entry.getValue().get(0).getQuotaname());
            step1.setIddepartment(entry.getValue().get(0).getIddepartment());
            step1.setNamedepartment(entry.getValue().get(0).getIdorganization());
            step1.setIdorganization(entry.getValue().get(0).getIdorganization());
            step1.setNameorganization(entry.getValue().get(0).getNameorganization());
            step1.setAmount(entry.getValue().stream().map(NKReceiptDto::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add));
            result.add(step1);
        }
        return result;
    }


}
